﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Public Class BH_BanHangNuocNgoai1
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim DienGiaiThue, MaDonVi, TenDonVi, DiaChi As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Dim flagTyGia As Boolean
    Private flag As Boolean = False 'tránh loop xtragrid
    Private m_MaPhieu As String = Nothing
    Public Sub setMa(ByVal ma As String)
        Me.m_MaPhieu = ma
    End Sub
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        If b Then
            Me.IGridViewHangHoa.OptionsBehavior.Editable = False
            Me.IGridViewChatLuong.OptionsBehavior.Editable = False
            Me.IGridViewThue.OptionsBehavior.Editable = False
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewChatLuong.OptionsBehavior.Editable = True
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
            Me.BarButtonItemTaoMoi.Enabled = False
            Me.BarButtonItemFirst.Enabled = False
            Me.BarButtonITemPrevious.Enabled = False
            Me.BarButtonItemNext.Enabled = False
            Me.BarButtonItemLast.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
            Me.BarButtonItemTaoMoi.Enabled = True
            Me.BarButtonItemFirst.Enabled = True
            Me.BarButtonITemPrevious.Enabled = True
            Me.BarButtonItemNext.Enabled = True
            Me.BarButtonItemLast.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
        Me.BarButtonItemChuyenDuLieuThue.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMCheBienTableAdapter.Fill(Me.PCV_FISHDataSet.DMCheBien)
        Me.DMBaoQuanTableAdapter.Fill(Me.PCV_FISHDataSet.DMBaoQuan)
        Me.DMPhamCapTableAdapter.Fill(Me.PCV_FISHDataSet.DMPhamCap)
        Me.DMKichCoTableAdapter.Fill(Me.PCV_FISHDataSet.DMKichCo)
        Me.Dmhanghoa_donvitinhTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa_donvitinh)
        Me.DmhanghoaTableAdapter.FillByMaTenDonViTinh(Me.PCV_FISHDataSet.dmhanghoa)
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        Me.BHBanHangTableAdapter.Fill(Me.PCV_FISHDataSet.BHBanHang, 2)
        Me.BHThueTableAdapter.Fill(Me.PCV_FISHDataSet.BHThue)
        Me.BHBanHangChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.BHBanHangChiTiet)
        Me.GetDMNhanVien_NhanVienBanHangTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNhanVien_NhanVienBanHang)
        Me.GetPhanLoai_ThanhToanTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_ThanhToan)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.GetPhanLoai_TienTeTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TienTe)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, Today)
        Me.GetDMNghiepVu_BanHangNuocNgoaiTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNghiepVu_BanHangNuocNgoai)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        setReadonlyControl(True)
        Me.BHBanHangIGridControl.ForceInitialize()
        Me.IGridViewDsBanHang.MoveLast()
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub BH_BanHangNuocNgoai_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If Me.flagTyGia Then
            Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        End If
        If Me.m_MaPhieu IsNot Nothing Then
            Dim position As Integer = Me.BHBanHangBindingSource.Find("ma", Me.m_MaPhieu)
            Me.BHBanHangBindingSource.Position = position
        End If
    End Sub

    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
        Me.IGridViewHangHoa.setBatLoi("taikhoanno", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanco", "not null")
        Me.IGridViewHangHoa.setBatLoi("mahang", "not null")
        Me.IGridViewHangHoa.setBatLoi("solo", "not null")
        Me.IGridViewHangHoa.setBatLoi("hsd", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanthueNK", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanchietkhau", "not null")
        Me.IGridViewHangHoa.setBatLoi("soluonggiao", "not 0 and not null")
        Me.IGridViewHangHoa.setBatLoi("soluongSO", "not 0 and not null")
        Me.ManghiepvuISearchLookupEdit.setNotShowPopUpEnter()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)

        'Mã //set ngầm
        Dim row As DataRowView = CType(BHBanHangBindingSource.AddNew(), DataRowView)
        Me.Ma = "BHNN" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        row("kyketoan") = Today.Year & Format(Today.Month, "00")
        row("loai") = 2
        'mặc định
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.NgayhoadonIDateEdit.EditValue = Today
        Me.NgaythanhtoanIDateEdit.EditValue = Today
        Me.NgaytokhaiHQIDateEdit.EditValue = Today
        Me.TygianganhangITextEdit.EditValue = 1
        Me.LoaitienISearchLookupEdit.EditValue = "VND"
        Me.ThanhtienvndITextEdit.EditValue = "0"
        Me.NguyenteITextEdit.EditValue = "0"
        Me.TinhtrangISearchLookupEdit.EditValue = "NL"
        Me.ManghiepvuISearchLookupEdit.Focus()
        If Me.ManghiepvuISearchLookupEdit.Focused Then
            ManghiepvuISearchLookupEdit.ShowPopup()
        End If
        Me.State = 1
    End Sub
    Private Sub setSuaNgam() 'set lại khi có sự thay đổi - set ngầm các trường ko hiện lên
        Dim row As DataRowView = CType(BHBanHangBindingSource.Current(), DataRowView)
        row("kyketoan") = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
    End Sub
    Private Sub setCongNoPhaiThu() 'xử lý công nợ phải thu 
        Dim sochungtu As String = SochungtuITextEdit.EditValue
        Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
        Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
        Dim madoituong As String = Me.MakhachhangISearchLookupEdit.EditValue
        Dim tendoituong As String = Me.MakhachhangISearchLookupEdit.Text
        Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
        Dim sohoadon As String = Me.SohoadonITextEdit.EditValue
        Dim ngayhoadon As Date = CDate(Me.NgayhoadonIDateEdit.EditValue)
        Dim hanthanhtoan As Date = CDate(NgaythanhtoanIDateEdit.EditValue)
        Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
        Dim tygia As Decimal = CDec(Me.TygianganhangITextEdit.EditValue)
        Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
        Dim thanhtienvnd As Decimal = CDec(Me.ThanhtienvndITextEdit.EditValue)
        Dim taikhoanno As String = Me.PCV_FISHDataSet.getDMNghiepVu_BanHangNuocNgoai.Rows(Me.GetDMNghiepVuBanHangNuocNgoaiBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanno").ToString
        Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
        'Me.GetTCCongNoPhaiThu_isTonTaiTableAdapter.Fill(Me.PCV_FISHDataSet.getTCCongNoPhaiThu_isTonTai, )
        If Me.GetTCCongNoPhaiThu_isTonTaiTableAdapter.getTCCongNoPhaiThu_isTonTai(Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma")) = 1 Then
            Me.TCCongNoPhaiThuTableAdapter.Update(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyente, thanhtienvnd, _
                                                  0, 0, 0, 0, 0, 0, taikhoanno, "", kyketoan, "", ma, getGuid(ma, 4), ma, getGuid(ma, 4), 0)
        Else
            Me.TCCongNoPhaiThuTableAdapter.Insert(sochungtu, ngaychungtu, madoituong, tendoituong, diachidoituong, sohoadon, ngayhoadon, hanthanhtoan, loaitien, tygia, 0, 0, 0, 0, nguyente, thanhtienvnd, _
                                                  0, 0, 0, 0, 0, 0, taikhoanno, "", kyketoan, "", ma, getGuid(ma, 4))
        End If
    End Sub
    Private Sub setTonKho()
        Try
            Dim sochungtu As String = SochungtuITextEdit.EditValue
            Dim ma As String = Me.IGridViewDsBanHang.GetFocusedRowCellValue("ma").ToString
            Dim ngaychungtu As Date = CDate(Me.NgaychungtuIDateEdit.EditValue)
            Dim madoituong As String = Me.MakhachhangISearchLookupEdit.EditValue
            Dim tendoituong As String = Me.MakhachhangISearchLookupEdit.Text
            Dim diachidoituong As String = Me.DiachiITextEdit.EditValue
            Dim sohoadon As String = Me.SohoadonITextEdit.EditValue
            Dim ngayhoadon As Date = CDate(Me.NgayhoadonIDateEdit.EditValue)
            Dim hanthanhtoan As Date = CDate(NgaythanhtoanIDateEdit.EditValue)
            'Dim loaitien As String = Me.LoaitienISearchLookupEdit.EditValue
            'Dim tygia As Decimal = CDec(Me.TygianganhangITextEdit.EditValue)
            'Dim nguyente As Decimal = CDec(Me.NguyenteITextEdit.EditValue)
            'Dim thanhtienvnd As Decimal = CDec(Me.ThanhtienvndITextEdit.EditValue)
            Dim taikhoanno As String = Me.PCV_FISHDataSet.getDMNghiepVu_BanHangNuocNgoai.Rows(Me.GetDMNghiepVuBanHangNuocNgoaiBindingSource.Find("ma", Me.ManghiepvuISearchLookupEdit.EditValue)).Item("taikhoanno").ToString
            Dim kyketoan As String = CDate(NgaychungtuIDateEdit.EditValue).Year & Format(CDate(NgaychungtuIDateEdit.EditValue).Month, "00")
            For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                Dim uid As System.Guid = Me.IGridViewHangHoa.GetRowCellValue(index, "uid")
                Dim mahang As String = Me.IGridViewHangHoa.GetRowCellValue(index, "mahang")
                Dim tenhang As String = Me.IGridViewHangHoa.GetRowCellValue(index, "tenhang")
                Dim quicach As String = Me.IGridViewHangHoa.GetRowCellValue(index, "quicach")
                Dim dvt As String = Me.IGridViewHangHoa.GetRowCellValue(index, "dvt")
                Dim solo As String = Me.IGridViewHangHoa.GetRowCellValue(index, "solo")
                Dim sologoc As String = Me.IGridViewHangHoa.GetRowCellValue(index, "sologoc")
                Dim hsd As Date = CDate(Me.IGridViewHangHoa.GetRowCellValue(index, "hsd"))
                Dim soluongxuat As Decimal = Me.IGridViewHangHoa.GetRowCellValue(index, "soluonggiao")
                Dim thanhtienxuat As Decimal = Me.IGridViewHangHoa.GetRowCellValue(index, "thanhtienvnd")
                Dim mafifo As String = mahang & solo & Format(hsd, "ddMMyyyy")
                If TCTonKhoTableAdapter.getTCTonKho_isTonTai(uid) = 1 Then
                    TCTonKhoTableAdapter.Update(sochungtu, ngaychungtu, sochungtu, ngaychungtu, mahang, tenhang, quicach, dvt, solo, sologoc, hsd, 0, 0, 0, 0, soluongxuat, thanhtienxuat, 0, 0, mafifo, kyketoan, "", ma, uid, uid, ma, 0)
                Else
                    TCTonKhoTableAdapter.Insert(sochungtu, ngaychungtu, sochungtu, ngaychungtu, mahang, tenhang, quicach, dvt, solo, sologoc, hsd, 0, 0, 0, 0, soluongxuat, thanhtienxuat, 0, 0, mafifo, kyketoan, "", ma, uid)
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewHangHoa.setUpdateData()
            Me.IGridViewChatLuong.setUpdateData()
            Me.IGridViewThue.setUpdateData()
            Me.BHBanHangBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            Me.FKBHThueBHBanHangBindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        setCongNoPhaiThu() 'xử lý insert or update vô bảng TCCongNoPhaiThu
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                        setTonKho() ' set tồn kho
                        setCongNoPhaiThu() 'set công nợ phải thu
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonlyControl(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
                Me.State = 0 'trở về trạng thái mặc định 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanged
        Dim view1 As GridView = CType(sender, GridView)
        Dim dongianguyente, dongiavnd, thanhtiennguyente, thanhtienvnd, tienthueGTGT As Decimal
        If (e.Column.FieldName = "soluonggiao" OrElse e.Column.FieldName = "dongiavnd" OrElse e.Column.FieldName = "dongianguyente" OrElse e.Column.FieldName = "tilethueGTGT") AndAlso (Not flag) AndAlso IsDBNull(e.Value) = False Then
            Try
                dongianguyente = CDec(view1.GetRowCellValue(e.RowHandle, view1.Columns("dongianguyente")))
                dongiavnd = dongianguyente * CDec(Me.TygianganhangITextEdit.EditValue)
                thanhtiennguyente = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongianguyente
                thanhtienvnd = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongiavnd
                tienthueGTGT = thanhtienvnd * CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("tilethueGTGT")) / 100
            Catch ex As Exception
                Exit Sub
            End Try
            flag = True
            view1.SetRowCellValue(e.RowHandle, view1.Columns("dongiavnd"), dongiavnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtiennguyente"), thanhtiennguyente)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtienvnd"), thanhtienvnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("tienthueGTGT"), tienthueGTGT)
            flag = False
        End If
        Me.NguyenteITextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtiennguyente").SummaryItem.SummaryValue)
        Me.ThanhtienvndITextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtienvnd").SummaryItem.SummaryValue)
        Me.IGridViewHangHoa.UpdateCurrentRow()
        Me.IGridViewHangHoa.UpdateSummary()
    End Sub

    Private Sub IGridViewHangHoa_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewHangHoa.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            Try
                If Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle) = "133" Then
                    Me.TienThue += CDec(val)
                    Me.DienGiaiThue = Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle, Me.IGridViewHangHoa.Columns("tenhang"))
                Else
                    Me.DoanhThu += CDec(val)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewHangHoa.InitNewRow
        'If State = 1 Then
        '    Me.IGridViewHangHoa.SetFocusedRowCellValue("ma", Ma)
        'End If
        Me.IGridViewHangHoa.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("soluongSO", 0) 'phân biệt chữ hoa thường
        Me.IGridViewHangHoa.SetFocusedRowCellValue("soluonggiao", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongianguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiavnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtiennguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienvnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueGTGT", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueGTGT", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chiphiban", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueNK", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueNK", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chietkhau", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chenhlech", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanno", TaiKhoanNo)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanco", TaiKhoanCo)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanthueGTGT", TaiKhoanThue)
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewHangHoa.MoveLast()
        Me.IGridViewHangHoa.Focus()
        Me.IGridViewHangHoa.AddNewRow()
        Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
        Me.IGridViewHangHoa.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditDMHangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.BHBanHangBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonITemPrevious.ItemClick
        Me.BHBanHangBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNext.ItemClick
        Me.BHBanHangBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLast.ItemClick
        Me.BHBanHangBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveFirst()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MovePrevious()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveNext()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveLast()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.BHBanHangBindingSource.RemoveCurrent()
            Me.BHBanHangTableAdapter.Update(Me.PCV_FISHDataSet.BHBanHang)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.BHBanHangBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewHangHoa.GetRowCellValue(Me.IGridViewHangHoa.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewHangHoa.DeleteRow(Me.IGridViewHangHoa.FocusedRowHandle)
                Me.IGridViewHangHoa.UpdateCurrentRow()
                Me.IGridViewHangHoa.UpdateGroupSummary()
                Me.IGridViewHangHoa.UpdateSummary()
            End If
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewThue.GetRowCellValue(Me.IGridViewThue.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewThue.DeleteRow(Me.IGridViewThue.FocusedRowHandle)
                Me.IGridViewThue.UpdateCurrentRow()
                Me.IGridViewThue.UpdateGroupSummary()
                Me.IGridViewThue.UpdateSummary()
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Dim tabname As String
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            tabname = "'Hàng hóa'"
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            tabname = "'Chất lượng'"
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            Me.FKBHThueBHBanHangBindingSource.EndEdit()
            tabname = "'Thuế'"
        End If
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần " & tabname & " không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.BHBanHangBindingSource.Position 'load dữ liệu lại nhưng giữ nguyên phiếu
        loadDuLieu()
        Me.BHBanHangBindingSource.Position = position
    End Sub

    Private Sub BarButtonItem5_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế tự động không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewHangHoa.Columns("taikhoanno").GroupIndex = 0
            Me.IGridViewHangHoa.Columns("sohoadon").GroupIndex = 1
            For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                Me.IGridViewHangHoa.MoveNext()
            Next
            Me.IGridViewHangHoa.ClearGrouping()
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(2) 'chọn tab thue
            Me.IGridViewThue.AddNewRow()
        End If
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        'set mặc định
        'Me.IGridViewThue.SetFocusedRowCellValue("ma", Ma)
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        '==================================================================
        Me.TongCong = CDec(Me.IGridViewHangHoa.Columns("thanhtienvnd").SummaryItem.SummaryValue)
        Try
            Me.PhanTramThue = Me.TienThue / Me.DoanhThu * 100
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Me.PhanTramThue = 0
        End Try
        Me.IGridViewThue.SetFocusedRowCellValue("diengiaithue", Me.DienGiaiThue)
        Dim position As Integer = Me.DmkhachhangBindingSource.Find("ma", Me.MakhachhangISearchLookupEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("madonvi", Me.MakhachhangISearchLookupEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("tendonvi", Me.MakhachhangISearchLookupEdit.Text)
        Me.IGridViewThue.SetFocusedRowCellValue("diachi", Me.DiachiITextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("masothue", Me.PCV_FISHDataSet.dmkhachhang.Rows(position).Item("masothue").ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
        Me.DoanhThu = 0
        Me.TienThue = 0
        Me.TongCong = 0
    End Sub
    Private Sub MakhachhangISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MakhachhangISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MakhachhangISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub

    Private Sub ManghiepvuISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles ManghiepvuISearchLookupEdit.EditValueChanging
        'xử lý ngầm tài khoản
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.ManghiepvuISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TaiKhoanNo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanno")
        Me.TaiKhoanCo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanco")
        Me.TaiKhoanThue = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanvat")
    End Sub

    Private Sub LoaitienISearchLookupEdit_AddNewValue(sender As Object, e As DevExpress.XtraEditors.Controls.AddNewValueEventArgs) Handles LoaitienISearchLookupEdit.AddNewValue
        DM_TyGia.Show()
        DM_TyGia.MdiParent = Me.MdiParent
        DM_TyGia.WindowState = FormWindowState.Maximized
        Me.flagTyGia = True
    End Sub

    Private Sub LoaitienISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles LoaitienISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.LoaitienISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TygianganhangITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tygia")
    End Sub

    Private Sub LoaitienISearchLookupEdit_Enter(sender As Object, e As EventArgs) Handles LoaitienISearchLookupEdit.Enter
        If Me.State <> 0 Then
            'Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, CDate(Me.NgaychungtuDateEdit.EditValue))
            Me.DmtygiaBindingSource.Filter = " ngaytygia = '" & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy-MM-dd") & "' or loaitien = 'VND'"
        End If
    End Sub

    Private Sub TinhtrangISearchLookupEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangISearchLookupEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0) 'chọn tab hàng hóa
            Me.BHBanHangBindingSource.EndEdit()
            Me.IGridViewHangHoa.setFocusDongDauTien()
        End If
    End Sub

    Private Sub BarButtonItemIn_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        Try
            Dim report As rp_BH_BanHangNuocNgoai1 = New rp_BH_BanHangNuocNgoai1()
            report.BhBanHangTableAdapter1.FillBySoChungTu(report.PcV_FISHDataSet1.BHBanHang, Me.SochungtuITextEdit.EditValue)
            report.BhBanHangChiTietTableAdapter1.Fill(report.PcV_FISHDataSet1.BHBanHangChiTiet)
            report.ShowRibbonPreview() 'hiển thị report theo kiểu ribbon
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MachuongtrinhkhuyenmaiISearchLookupEdit_Enter(sender As Object, e As EventArgs) Handles MachuongtrinhkhuyenmaiISearchLookupEdit.Enter
        If State <> 0 Then
            Me.GetBHChuongTrinhKhuyenMai_BHChuongTrinhKhuyenMaiDoiTuongApDungTableAdapter.Fill(Me.PCV_FISHDataSet.getBHChuongTrinhKhuyenMai_BHChuongTrinhKhuyenMaiDoiTuongApDung, Me.MakhachhangISearchLookupEdit.EditValue, CDate(Me.NgaychungtuIDateEdit.EditValue))
        End If
    End Sub

    Private Sub BanggiaISearchLookupEdit_Enter(sender As Object, e As EventArgs) Handles BanggiaISearchLookupEdit.Enter
        If State <> 0 Then
            Me.GetBHBangGia_BHBangGiaDoiTuongApDungTableAdapter.Fill(Me.PCV_FISHDataSet.getBHBangGia_BHBangGiaDoiTuongApDung, Me.MakhachhangISearchLookupEdit.EditValue, CDate(Me.NgaychungtuIDateEdit.EditValue))
        End If
    End Sub
End Class
